package common

import (
	"fmt"
	"github.com/google/uuid"
	"math/rand"
	"os/exec"
	"strings"
	"time"
)

func GenerateFileName() string {
	// 使用UUID生成唯一的文件名
	uuidFileName := uuid.New().String() + ".jpg"
	return uuidFileName
}

func GenerateTimestampFileName() string {
	// 使用当前时间戳+UUID生成文件名
	timestamp := time.Now().Format("20060102150405")
	timestampFileName := fmt.Sprintf("%s_%s.jpg", timestamp, uuid.New().String())
	return timestampFileName
}

func GetDeviceSerial() string {
	cmd := exec.Command("sh", "-c", "lshw -C system | grep serial")
	output, _ := cmd.Output()
	parts := strings.Split(string(output), ": ")
	serialNumber := strings.TrimSpace(parts[1])
	Logger.Infof("设备板卡SN: %v", serialNumber)
	return serialNumber
}

func GenerateMsgId() string {
	// 生成7位随机字符串
	const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
	var randomStr strings.Builder
	for i := 0; i < 7; i++ {
		randomStr.WriteByte(chars[rand.Intn(len(chars))])
	}
	utcTs := time.Now().UnixMilli()
	// 组合 msgId
	msgId := fmt.Sprintf("%d%s", utcTs, randomStr.String())
	return msgId
}

func GenerateOssStorePath(devId string, fileName string) string {
	dataPart := time.Now().Format("20060102")
	path := fmt.Sprintf("picure/1/%s/%s/%s", devId, dataPart, fileName)
	return path
}

// 遍历map得到keys的集合
func MapKeys[V any](m map[string]V) []string {
	keys := make([]string, 0, len(m))
	for k := range m {
		keys = append(keys, k)
	}
	return keys
}
